<?php
class pmtData 
{

	var $myconn;
	
	// Instantiating this class will immediately generate a database connection.
	public function __construct($dbHost,$dbUser,$dbPass,$dbName) {
		   
		$conn = mysqli_connect($dbHost,$dbUser,$dbPass,$dbName) or die("Error " . mysqli_error($conn));
		
		$this->myconn = $conn;
		
	}  
    
	// de-instantiation will result in closing the connection.
    public function __destruct() {   
    	
    	//mysqli_close($this->myconn);
    	
    }
      
    public function close() {
    	 
    	mysqli_close($this->myconn);
    
    }
    
    public function retrieve($select, $from, $where = null, $orderby = null) {
    	
    	//echo "building query <br />";
    	//echo "SELECT: " . $select . "<br />";
    	//echo "FROM: " . $from . "<br />";
    	//echo "WHERE: " . $where . "<br />";
    	//echo "ORDERBY: " . $orderby . "<br />";
    	
    	$query = "SELECT ". $select ." FROM ". $from;
    	
    	if($where) {
    		$query = $query . " WHERE ". $where;
    	}

    	if($orderby) {
    		$query = $query . " ORDER BY ". $orderby;
    	}
    	
    	//echo "Query: " . $query . "<br />";
    	
    	$result = mysqli_query($this->myconn,$query);
    	
    	if (!$result) {
    		die("Could not perform MySQL query.");
    	}
    	
    	//echo "Er is resultaat! <br />";
    	
    	return $result;
    	
    }  

    public function getDeckList() {
    	    	
    	$fetch = self::retrieve("*","pmtdeck");
    	    	
    	//Code to transform retrieved data to XML
    	$decklist = new DOMDocument("1.0", "UTF-8");
    	$xmlroot = $decklist->createElement("decks");
    	$xmlroot = $decklist->appendChild($xmlroot);
    	    	
    	while ($val = mysqli_fetch_array($fetch)) {
    		//print_r ($val);
    		$currentTrack = $decklist->createElement("deck");
    		$currentTrack = $xmlroot->appendChild($currentTrack);
    		$currentTrack->appendChild($decklist->createElement("id",$val['id']));
    		$currentTrack->appendChild($decklist->createElement("name",$val["name"]));
    		$currentTrack->appendChild($decklist->createElement("description",$val["description"]));
    		$currentTrack->appendChild($decklist->createElement("shortDescription",$val["shortDescription"]));
    		$currentTrack->appendChild($decklist->createElement("status",$val["status"]));
    		$currentTrack->appendChild($decklist->createElement("themeID",$val["themeID"]));
    	}
    	
    	return $decklist->saveXML(); //as a DOMDocument
    }
    
    public function getDeck($deckID) {
    	
    	$fetch = self::retrieve("*","pmtdeck","id=".$deckID);

    	//Code to transform retrieved data to XML
    	$deck = new DOMDocument("1.0", "UTF-8");
    	$xmlroot = $deck->createElement("deck");
    	$xmlroot = $deck->appendChild($xmlroot);
    	
    	$record = mysqli_fetch_array($fetch);
   		$xmlroot->appendChild($deck->createElement("id",$record['id']));
   		$xmlroot->appendChild($deck->createElement("name",$record["name"]));
   		$xmlroot->appendChild($deck->createElement("description",$record["description"]));
   		$xmlroot->appendChild($deck->createElement("shortDescription",$record["shortDescription"]));
   		$xmlroot->appendChild($deck->createElement("status",$record["status"]));
   		$xmlroot->appendChild($deck->createElement("themeID",$record["themeID"]));
   		
    	return $deck->saveXML(); //as a DOMDocument
    	 
    }
    
    public function setDeck($deck) {
    	
       	//$deck is delivered as a DOMDocument
    	//If ID is empty, it's a new document.
    	//Return value is deckID
    	
    	// Set return value to 0 by default. Will be set to deckID when the operation is succesful
    	$val = null;

    	// Put content into array
    	$deckElement = $deck->documentElement;
    	$deckArray = array();
    	foreach ($deckElement->childNodes as $deckField) {
    		$deckArray[$deckField->nodeName] = $deckField->nodeValue;
    	}

    	if ($deckArray["id"] == "") {

    		//No deckID included, new record
    		
    		$query = "INSERT INTO pmtdeck (`name`, `description`, `shortDescription`, `status`, `themeID`) 
    					VALUES ('".$deckArray["name"]."','".$deckArray["description"]."','".$deckArray["shortDescription"]."','".$deckArray["status"]."','".$deckArray["themeID"]."');";
    		$result = mysqli_query($this->myconn,$query);
    		echo "<br />Query: " . $query . "<br />"; 
    		if ($result) {
    			$val = mysqli_insert_id($this->myconn);
    		} else {
    			$val = null;
    		}
    		
    	} else { 
    		// ID included, update record
    		echo "Met ID <br />";
    		$fetch = self::retrieve("*","pmtdeck","id=".$deckArray["id"]);
    	
    		if ($fetch) {
    			// If exists: Update record
    		   	$query = "UPDATE pmtdeck
    		   				SET name = '".$deckArray["name"]."' 
    		   					description = '".$deckArray["description"]."'
    		   					shortDescription = '".$deckArray["shortDescription"]."'
    		   					status = '".$deckArray["status"]."'
    		   					themeID = '".$deckArray["themeID"]."'
    		   				WHERE id = '".$deckArray["id"]."';";
    		   	$result = mysqli_query($this->myconn,$query);
    			
    		   	if ($result) {
    				$val = $deckArray["id"];
    		   	} else {
    		   		$val = null;
    		   	}
    		} else {
    			$val = null;
    		}
    		
    	}
    	
    	return $val;
    	
    }
    
	public function getStackList() {
    	    	
    	$fetch = self::retrieve("*","pmtstack");
    	    	
    	//Code to transform retrieved data to XML
    	$stackList = new DOMDocument("1.0", "UTF-8");
    	$xmlStackroot = $stackList->createElement("stacks");
    	$xmlStackroot = $stackList->appendChild($xmlStackroot);
    	    	
    	while ($val = mysqli_fetch_array($fetch)) {
    		//print_r ($val);
    		$currentStackTrack = $stackList->createElement("stack");
    		$currentStackTrack = $xmlStackroot->appendChild($currentStackTrack);
    		$currentStackTrack->appendChild($stackList->createElement("stackID",$val['id']));
    		$currentStackTrack->appendChild($stackList->createElement("stackName",$val["name"]));
    		$currentStackTrack->appendChild($stackList->createElement("stackDescription",$val["description"]));
    		$currentStackTrack->appendChild($stackList->createElement("stackShortDescription",$val["shortDescription"]));
    		$currentStackTrack->appendChild($stackList->createElement("stackStatus",$val["status"]));
    		$currentStackTrack->appendChild($stackList->createElement("deckID",$val["deckID"]));
    	}
    	
    	return $stackList->saveXML(); //as a DOMDocument
    	
    }
    
    public function getStackByID($stackID) {
    	$fetch = self::retrieve("*","pmtstack","id=".$stackID);
    	
    	//Code to transform retrieved data to XML
    	$stack = new DOMDocument("1.0", "UTF-8");
    	$xmlroot = $stack->createElement("stack");
    	$xmlroot = $stack->appendChild($xmlroot);
    	 
    	$record = mysqli_fetch_array($fetch);
    	$xmlroot->appendChild($stack->createElement("stackID",$record['id']));
    	$xmlroot->appendChild($stack->createElement("stackName",$record["name"]));
    	$xmlroot->appendChild($stack->createElement("stackDescription",$record["description"]));
    	$xmlroot->appendChild($stack->createElement("stackShortDescription",$record["shortDescription"]));
    	$xmlroot->appendChild($stack->createElement("stackStatus",$record["status"]));
    	$xmlroot->appendChild($stack->createElement("deckID",$record["deckID"]));
    	
    	return $stack->saveXML(); //as a DOMDocument
    }
    
    public function getStacksByDeckID($deckID) {
    	$fetch = self::retrieve("*","pmtstack","deckID=".$deckID);
    	 
    	//Code to transform retrieved data to XML
    	$stackList = new DOMDocument("1.0", "UTF-8");
    	$xmlStackroot = $stackList->createElement("stacks");
    	$xmlStackroot = $stackList->appendChild($xmlStackroot);
    	    	
    	while ($val = mysqli_fetch_array($fetch)) {
    		//print_r ($val);
    		$currentStackTrack = $stackList->createElement("stack");
    		$currentStackTrack = $xmlStackroot->appendChild($currentStackTrack);
    		$currentStackTrack->appendChild($stackList->createElement("stackID",$val['id']));
    		$currentStackTrack->appendChild($stackList->createElement("stackName",$val["name"]));
    		$currentStackTrack->appendChild($stackList->createElement("stackDescription",$val["description"]));
    		$currentStackTrack->appendChild($stackList->createElement("stackShortDescription",$val["shortDescription"]));
    		$currentStackTrack->appendChild($stackList->createElement("stackStatus",$val["status"]));
    		$currentStackTrack->appendChild($stackList->createElement("deckID",$val["deckID"]));
    	}
    	
    	return $stackList->saveXML(); //as a DOMDocument
    }
    
    public function setStack($stack) {

    	//$stack is delivered as a DOMDocument
    	//If ID is empty, it's a new document.
    	//Return value is stackID
    	 
    	// Set return value to 0 by default. Will be set to deckID when the operation is succesful
    	$val = null;
    	
    	// Put content into array
    	$stackElement = $stack->documentElement;
    	$stackArray = array();
    	foreach ($stackElement->childNodes as $stackField) {
    		$stackArray[$stackField->nodeName] = $stackField->nodeValue;
    	}
    	
    	if ($stackArray["id"] == "") {
    	
    		//No stackID included, new record
    	
    		$query = "INSERT INTO pmtstack (`deckID`, `name`, `description`, `shortDescription`, `status`)
    					VALUES ('".$stackArray["deckID"]."','".$stackArray["name"]."','".$stackArray["description"]."','".$stackArray["shortDescription"]."','".$stackArray["status"]."');";
    		$result = mysqli_query($this->myconn,$query);
    		echo "<br />Query: " . $query . "<br />";
    		if ($result) {
    			$val = mysqli_insert_id($this->myconn);
    		} else {
    			$val = null;
    		}
    	
    	} else {
    		// ID included, update record
    		echo "Met ID <br />";
    		$fetch = self::retrieve("*","pmtstack","id=".$stackArray["id"]);
    		 
    		if ($fetch) {
    			// If exists: Update record
    			$query = "UPDATE pmtstack
    		   				SET deckID = '".$stackArray["deckID"]."'
    							name = '".$stackArray["name"]."'
    		   					description = '".$stackArray["description"]."'
    		   					shortDescription = '".$stackArray["shortDescription"]."'
    		   					status = '".$stackArray["status"]."'
    		   				WHERE id = '".$stackArray["id"]."';";
    			$result = mysqli_query($this->myconn,$query);
    			 
    			if ($result) {
    				$val = $stackArray["id"];
    			} else {
    				$val = null;
    			}
    		} else {
    			$val = null;
    		}
    	
    	}
    	 
    	return $val;
    	 
    }
    

    public function getCardList() {
    
    	$fetch = self::retrieve("*","pmtcard");
    
    	//Code to transform retrieved data to XML
    	$cardList = new DOMDocument("1.0", "UTF-8");
    	$xmlCardroot = $cardList->createElement("cards");
    	$xmlCardroot = $cardList->appendChild($xmlCardroot);
    
    	while ($val = mysqli_fetch_array($fetch)) {
    		//print_r ($val);
    		$currentCardTrack = $cardList->createElement("stack");
    		$currentCardTrack = $xmlCardroot->appendChild($currentCardTrack);
    		$currentCardTrack->appendChild($cardList->createElement("cardID",$val['id']));
    		$currentCardTrack->appendChild($cardList->createElement("cardName",$val["name"]));
    		$currentCardTrack->appendChild($cardList->createElement("cardDescription",$val["description"]));
    		$currentCardTrack->appendChild($cardList->createElement("cardShortDescription",$val["shortDescription"]));
    		$currentCardTrack->appendChild($cardList->createElement("cardStatus",$val["status"]));
    		$currentCardTrack->appendChild($cardList->createElement("stackID",$val["stackID"]));
    	}
    	 
    	return $cardList->saveXML(); //as a DOMDocument
    	 
    }
    
    public function getCard($cardID) {
    	$fetch = self::retrieve("*","pmtcard","id=".$cardID);
    	 
    	//Code to transform retrieved data to XML
    	$card = new DOMDocument("1.0", "UTF-8");
    	$xmlroot = $card->createElement("card");
    	$xmlroot = $card->appendChild($xmlroot);
    
    	$record = mysqli_fetch_array($fetch);
    	$xmlroot->appendChild($card->createElement("cardID",$record['id']));
    	$xmlroot->appendChild($card->createElement("cardkName",$record["name"]));
    	$xmlroot->appendChild($card->createElement("cardDescription",$record["description"]));
    	$xmlroot->appendChild($card->createElement("cardShortDescription",$record["shortDescription"]));
    	$xmlroot->appendChild($card->createElement("cardStatus",$record["status"]));
    	$xmlroot->appendChild($card->createElement("stackID",$record["stackID"]));
    	 
    	return $card->saveXML(); //as a DOMDocument
    }
    
    public function getCardByStackID($stackID) {
    	$fetch = self::retrieve("*","pmtcard","stackID=".$stackID);
    
    	//Code to transform retrieved data to XML
    	$cardList = new DOMDocument("1.0", "UTF-8");
    	$xmlCardroot = $cardList->createElement("cards");
    	$xmlCardroot = $cardList->appendChild($xmlCardroot);
    
    	while ($val = mysqli_fetch_array($fetch)) {
    		//print_r ($val);
    		$currentCardTrack = $cardList->createElement("stack");
    		$currentCardTrack = $xmlCardroot->appendChild($currentCardTrack);
    		$currentCardTrack->appendChild($cardList->createElement("cardID",$val['id']));
    		$currentCardTrack->appendChild($cardList->createElement("cardName",$val["name"]));
    		$currentCardTrack->appendChild($cardList->createElement("cardDescription",$val["description"]));
    		$currentCardTrack->appendChild($cardList->createElement("cardShortDescription",$val["shortDescription"]));
    		$currentCardTrack->appendChild($cardList->createElement("cardStatus",$val["status"]));
    		$currentCardTrack->appendChild($cardList->createElement("stackID",$val["stackID"]));
    	}
    	 
    	return $cardList->saveXML(); //as a DOMDocument
    }
    
    public function setCard($card) {
    	//$card is delivered as a DOMDocument
    	//If ID is empty, it's a new document.
    	//Return value is stackID
    	 
    	// Set return value to 0 by default. Will be set to deckID when the operation is succesful
    	$val = null;
    	
    	// Put content into array
    	$cardElement = $card->documentElement;
    	$cardArray = array();
    	foreach ($cardElement->childNodes as $cardField) {
    		$cardArray[$cardField->nodeName] = $cardField->nodeValue;
    	}
    	
    	if ($cardArray["id"] == "") {
    	
    		//No stackID included, new record
    	
    		$query = "INSERT INTO pmtcard (`stackID`, `parentID`, `name`, `description`, `shortDescription`, `status`, `value`, `effort`, `assignedTo`)
    					VALUES ('".$cardArray["stackID"]."','".$cardArray["parentID"]."','".$cardArray["name"]."','".$cardArray["description"]."','".$cardArray["shortDescription"]."','".$cardArray["status"]."','".$cardArray["value"]."','".$cardArray["effort"]."','".$cardArray["assignedTo"]."');";
    		$result = mysqli_query($this->myconn,$query);
    		echo "<br />Query: " . $query . "<br />";
    		if ($result) {
    			$val = mysqli_insert_id($this->myconn);
    		} else {
    			$val = null;
    		}
    	
    	} else {
    		// ID included, update record
    		echo "Met ID <br />";
    		$fetch = self::retrieve("*","pmtcard","id=".$cardArray["id"]);
    		 
    		if ($fetch) {
    			// If exists: Update record
    			$query = "UPDATE pmtcard
    		   				SET stackID = '".$cardArray["stackID"]."'
    		   					parentID = '".$cardArray["parentID"]."'
    							name = '".$cardArray["name"]."'
    		   					description = '".$cardArray["description"]."'
    		   					shortDescription = '".$cardArray["shortDescription"]."'
    		   					status = '".$cardArray["status"]."'
    		   					value = '".$cardArray["value"]."'
    		   					effort = '".$cardArray["effort"]."'
    		   					assignedTo = '".$cardArray["assignedTo"]."'
    		   				WHERE id = '".$cardArray["id"]."';";
    			$result = mysqli_query($this->myconn,$query);
    			 
    			if ($result) {
    				$val = $cardArray["id"];
    			} else {
    				$val = null;
    			}
    		} else {
    			$val = null;
    		}
    	
    	}
    	 
    	return $val;
    	 
    	
    }
    
    
}
